﻿
function renameTreeNode(e, data)
{
    $.post(
		"/Asset/AjaxAsset.aspx", 
		{ 
			"operation" : "rename_node", 
			"id" : data.rslt.obj.attr("id").replace("node_",""),
			"title" : data.rslt.new_name
		}, 
		function (r) {
			if(!r.status) {
				$.jstree.rollback(data.rlbk);
				if(r.error != "")
				{
				    alert(r.error);
				}
			}
		}
	);
}

function removeTreeNode(e, data)
{
    data.rslt.obj.each(function () {
	    $.ajax({
		    async : false,
		    type: 'POST',
		    url: "/Asset/AjaxAsset.aspx",
		    data : { 
			    "operation" : "remove_node", 
			    "id" : this.id.replace("node_","")
		    }, 
		    success : function (r) {
			    if(!r.status) {
				    $.jstree.rollback(data.rlbk);
				    if(r.error != "")
			        {
			            alert(r.error);
			        }
			    }
		    }
	    });
    });
}

function createTreeNode(e, data)
{
    $.post(
		"/Asset/AjaxAsset.aspx", 
		{ 
			"operation" : "create_node", 
			"id" : data.rslt.parent.attr("id").replace("node_",""), 
			"position" : data.rslt.position,
			"title" : data.rslt.name,
			"type" : data.rslt.obj.attr("rel")
		}, 
		function (r) {
		    if(r.status) {
				$(data.rslt.obj).attr("id", r.id);
			}
			else {
				$.jstree.rollback(data.rlbk);
				if(r.error != "")
				{
				    alert(r.error);
				}
			}
		}
	);
}

function moveTreeNode(e, data)
{
    data.rslt.o.each(function (i) {
		$.ajax({
			async : false,
			type: 'POST',
			url: "/Asset/AjaxAsset.aspx",
			data : { 
				"operation" : "move_node", 
				"id" : $(this).attr("id").replace("node_",""), 
				"ref" : data.rslt.cr === -1 ? 1 : data.rslt.np.attr("id").replace("node_",""), 
				"position" : data.rslt.cp + i,
				"title" : data.rslt.name,
				"copy" : data.rslt.cy ? 1 : 0
			},
			success : function (r) {
				if(!r.status) {
					$.jstree.rollback(data.rlbk);
					if(r.error != "")
					{
					    alert(r.error);
					}
				}
				else {
					$(data.rslt.oc).attr("id", "node_" + r.id);
					if(data.rslt.cy && $(data.rslt.oc).children("UL").length) {
						data.inst.refresh(data.inst._get_parent(data.rslt.oc));
					}
				}
				$("#analyze").click();
			}
		});
	});
}